package com.example.thegreatwall.controller;


import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.thegreatwall.common.R;
import com.example.thegreatwall.entity.UContacts;
import com.example.thegreatwall.service.UContactsService;
import com.example.thegreatwall.utils.VerifyIdCard;
import org.apache.ibatis.annotations.Param;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
@RequestMapping("/ucontacts")
@CrossOrigin(origins = "*") //无论什么源都可以访问  不写这个不允许跨域
@RestController
public class UContactsController{
    @Autowired
    private UContactsService uContactsService;

    /**
     * 添加常用联系人
     * @param ucontacts 常用联系人对象
     * @return
     */
    @PostMapping("/addUConcacts")
    public R addUContacts(@RequestBody UContacts ucontacts){
        VerifyIdCard.setName(ucontacts.getName());//传名字
        VerifyIdCard.setTestIdCard(ucontacts.getIdcard());//传身份证
        VerifyIdCard.main(null);//调用main方法
        String msg = VerifyIdCard.getMsg();//得到json字符串
        JSONObject jsonObject = JSONObject.parseObject(msg);
        int code = jsonObject.getInteger("code");
        if (code==200){
            JSONObject data = jsonObject.getJSONObject("data");
            if (data.getString("desc").equals("一致")){
                System.out.print("true");
                //先查询是否数据库已经存在这个联系人，存在则做修改
                QueryWrapper<UContacts> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("idCard",ucontacts.getIdcard()).and(i->i.eq("user_id",ucontacts.getUserId()));
                UContacts uContacts = uContactsService.getOne(queryWrapper);
                if(uContacts!=null){//查询出对应身份证，修改
                    uContacts.setName(ucontacts.getName());
                    uContacts.setPhone(ucontacts.getPhone());
                    uContacts.setRole(ucontacts.getRole());
                    uContacts.setCertificateNum(ucontacts.getCertificateNum());
                    boolean b = uContactsService.updateById(uContacts);
                    if(b){
                        return new R().setMsg("success");
                    }else {
                        return new R().setMsg("fail");
                    }
                }else {//不存在对应身份证，新增
                    boolean save = uContactsService.save(ucontacts);
                    if(save){
                        return new R().setMsg("success");
                    }else {
                        return new R().setMsg("fail");
                    }
                }
            }else {
                System.out.println("名字不正确");
                return new R().setMsg("名字不正确");
            }
        }else {
            System.out.println(jsonObject.getString("msg"));
            return new R().setMsg("开出隐藏结局了fail ");
        }
    }

    /**
     * 根据用户id查询常用联系人
     * @param uid 用户id
     * @return
     */
    @GetMapping("/getUContatesById")
    public R getUContatesById(@RequestParam int uid){
        QueryWrapper<UContacts> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", uid);
        List<UContacts> list = uContactsService.list(queryWrapper);
        return new R().addDate("list", list);
    }

    /**
     * 根据id查询常用联系人
     * @param arr 常用联系人idstr 逗号分隔,如 ：1,2,3,4
     * @return
     */
    @GetMapping("/getContatesByList")
    public R getContatesByList(@RequestParam String arr){
        String[] split = arr.split(",");
        List<Integer> list=new ArrayList<>();
        for (String s : split) {
            list.add(Integer.parseInt(s.trim()));
        }
        List<UContacts> uContacts = uContactsService.selectContactsByUserId(list);
        return new R().addDate("list", uContacts);
    }

    /**
     * 根据身份证查询联系人信息
     * @param idcardArr 常用联系人身份证str 逗号分隔
     * @return
     */
    @GetMapping("/getContatesByIdcard")
    public R getContatesByIdcard(@RequestParam("idcardArr") String idcardArr,@RequestParam("userid") int userid){
        System.out.println("你妹嘟。。。。"+userid);
        String[] split = idcardArr.split(",");
        List<String> list=new ArrayList<>();
        for (String s : split) {
            list.add(s.trim());
        }
        List<UContacts> uContacts = uContactsService.selectContactsByIdcard(list,userid);
        System.out.println(uContacts);
        return new R().addDate("list", uContacts);
    }

    /**
     * 查看订单里面的常用联系人
     * @param ids
     * @return
     */
    @GetMapping(value = "ids")
    public R selectContactsByIds(@RequestParam List<Integer> ids){
        return new R().addDate("list", uContactsService.selectContactsByIds(ids));
    }

    @GetMapping("/getcontates")
    public R selectContactsByIds(@RequestParam("ids") String ids){
        List<Integer> list=new ArrayList<>();
        String[] split = ids.split(",");
        for (String s : split) {
            list.add(Integer.parseInt(s.trim()));
        }
        return new R().addDate("list", uContactsService.selectContactsByIds(list));
    }

    @GetMapping("/delUComtateById")
    public R delUComtateById(@RequestParam("id") int id){
        boolean b = uContactsService.removeById(id);
        return new R().setMsg("success");
    }
}

